package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class Ai0455NewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*.ai0455.com/contents/[0-9]+/[0-9]+.html",

				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";;
		String author="";
		String url=urlMeta.getUrl();
		Long date=null;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		Document doc=Jsoup.parse(html);


		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		ParseTool tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("td[align=center] h2"));
		title=tool.parse();

		/*
		 * 解析时间
		 */
		tool.addParseLogic(new ParseLogic("td[align=center]:containsOwn(时间)"));
		date=tool.parseDate();


		content=HtmlCleaner.getContentHtml(url,doc.select("td[valign=top][height=300]"));

		/*
		 * 解析作者
		 */
		 author= JSoupUtils.matchAuthor(doc, "来源：");
		if(StringUtils.isNotBlank(author)){
			author=author.trim();
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setAuthor(author);

		return meta;


	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		String url="http://wzkx.ai0455.com/contents/147/48069.html";
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		Ai0455NewsAnalyse test=new Ai0455NewsAnalyse();
		boolean detailPage=test.isDetailPage(url);
		if(detailPage){
		NewsMeta parserHtml =test.parserHtml(urlMeta);
		System.out.println(parserHtml);
		}else{
			System.out.println("不符合正则");
		}
	}



	
	public boolean isNeedUpdate(){
		return false;
	}

}
